/**
 * 
 */
package mx.frantor.catalogos.daoimp;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import mx.frantor.catalogos.dao.CatalogoProductoDAO;
import mx.frantor.catalogos.entitys.CatalogoProductoEntity;
import mx.frantor.util.HibernateUtil;

/**
 * @author Hp
 *
 */
public class CatalogoProductoDAOImpl implements CatalogoProductoDAO {

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.CatalogoProductoDAO#agregarCatalogoP(mx.frantor.catalogos.entitys.CatalogoProductoEntity)
	 */
	@Override
	public String agregarCatalogoP(CatalogoProductoEntity nuevoCatalogo) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		
		session.beginTransaction();
		try {
		session.save(nuevoCatalogo);
		session.flush();
		session.getTransaction().commit();
		e = nuevoCatalogo.getIdCatalogo().toString();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("agregar catalogo producto resultado " + e);

		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.CatalogoProductoDAO#modificarCatalogoP(mx.frantor.catalogos.entitys.CatalogoProductoEntity)
	 */
	@Override
	public String modificarCatalogoP(CatalogoProductoEntity modificarCatalogoP) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
	
		session.beginTransaction();
		try {
			CatalogoProductoEntity modCatalogoemp = (CatalogoProductoEntity) session.get(
					CatalogoProductoEntity.class, modificarCatalogoP.getIdCatalogo());
		session.flush();
		session.evict(modCatalogoemp);
		session.update(modificarCatalogoP);
		session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("modificar catalogo producto resultado " + e);

		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.CatalogoProductoDAO#eliminarCatalogoP(java.lang.Integer)
	 */
	@Override
	public String eliminarCatalogoP(long idCatalogoP) {
		// TODO Auto-generated method stub
		String e = "";
		Session session = HibernateUtil.getSessionFactory()
				.getCurrentSession();
		

			// TODO Auto-generated method stub			
			session.beginTransaction();
			try {
				CatalogoProductoEntity CatalogoPElimina = (CatalogoProductoEntity) session.get(
						CatalogoProductoEntity.class, idCatalogoP);
			session.delete(CatalogoPElimina);
			session.getTransaction().commit();
		} catch (Exception x) {
			//e.printStackTrace();
			session.getTransaction().rollback();
			e = x.getMessage();
			//return e.toString();
		}
		//HibernateUtil.getSessionFactory().getCurrentSession().close();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		System.out.println("eliminar catalogo producto resultado " + e);

		return e;
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.CatalogoProductoDAO#getTodosCatalogoPActivos()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CatalogoProductoEntity> getTodosCatalogoPActivos() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<CatalogoProductoEntity> catalp = session.createCriteria(CatalogoProductoEntity.class)
				.addOrder(Order.asc("IdCatalogo")).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return catalp;
		}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.CatalogoProductoDAO#getCatalogoPId(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public CatalogoProductoEntity getCatalogoPId(long idCatalogoP) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<CatalogoProductoEntity> catalogop = (List<CatalogoProductoEntity>) session.createCriteria(CatalogoProductoEntity.class)
				.add(Restrictions.eq("IdCatalogo", idCatalogoP))
				.setFetchSize(1).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		if(catalogop==null){
			return null;
		}else{
			return catalogop.get(0);
		}	
	}

	/* (non-Javadoc)
	 * @see mx.frantor.catalogos.dao.CatalogoProductoDAO#getCatalogoPPorId(java.lang.Integer)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<CatalogoProductoEntity> getCatalogoPPorId(long idCatalogoP) {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<CatalogoProductoEntity> catprod = (List<CatalogoProductoEntity>) session.createCriteria(CatalogoProductoEntity.class)
				.add(Restrictions.eq("IdCatalogo", idCatalogoP))
				.list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return catprod;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CatalogoProductoEntity> getCatalogoPPorIdProducto(
			long idProducto) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<CatalogoProductoEntity> remanuf = session.createCriteria(CatalogoProductoEntity.class)
				.add(Restrictions.eq("IdProducto", idProducto)).list();
		HibernateUtil.getSessionFactory().getCurrentSession().close();
		return remanuf;
	}
	

}
